/**
 * 文件名称:  CusDataSource.java
 * 文件序号:  序号
 * 内容描述:  文件简单描述，多行换行书写
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  zhangkai
 * 创建日期:  2016/12/20 17:21
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:  V1.0
 * 文件路径:
 */

package com.hongdee.common.config;


import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.util.Properties;


/**
 * 名  称:CusDataSource
 * 类  型: class
 * 描  述: 用一句话描述类的作用
 * 路  径:com.hongdee.datasource
 * 作  者:zhangkai
 * 日  期:2016/12/20 17:21
 */


@Configuration
public class DataSourceConfig implements EnvironmentAware {
    /**
     *
     */

    private RelaxedPropertyResolver propertyResolver;


    /**
     * @desc 实现设置环境变量参数，用于读取配置文件信息
     * @author zhangkai
     * @date 2017/2/8
     * @time 18:17
     * @update_user
     * @update_time
     * @update_desc:
     */
    @Override
    public void setEnvironment(Environment environment) {
        this.propertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");

    }

    /**
     * @return 返回数据源配置
     * @desc 设置数据库操作的数据源信息
     * @author zhangkai
     * @date 2017/2/8
     * @time 18:17
     * @update_user
     * @update_time
     * @update_desc:
     */
    @Bean(name = "dataSource", destroyMethod = "close", initMethod = "init")
    @Qualifier("dataSource")
    @Primary
    public DataSource writeDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(propertyResolver.getProperty("primary.url"));
        datasource.setDriverClassName(propertyResolver.getProperty("primary.driver-class-name"));
        datasource.setUsername(propertyResolver.getProperty("primary.username"));
        datasource.setPassword(propertyResolver.getProperty("primary.password"));
        datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("primary.initialSize")));
        datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("primary.minIdle")));
        datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("primary.maxWait")));
        datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("primary.maxActive")));
        datasource.setMinEvictableIdleTimeMillis(Long.valueOf(propertyResolver.getProperty("primary.minEvictableIdleTimeMillis")));
        datasource.setDefaultAutoCommit(true);
        return datasource;
    }

    @Bean(name = "impDataSource", destroyMethod = "close", initMethod = "init")
    @Qualifier("impDataSource")
    public DataSource writeSecondDataSource() {
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(propertyResolver.getProperty("second.url"));
        datasource.setDriverClassName(propertyResolver.getProperty("second.driver-class-name"));
        datasource.setUsername(propertyResolver.getProperty("second.username"));
        datasource.setPassword(propertyResolver.getProperty("second.password"));
        datasource.setInitialSize(Integer.valueOf(propertyResolver.getProperty("second.initialSize")));
        datasource.setMinIdle(Integer.valueOf(propertyResolver.getProperty("second.minIdle")));
        datasource.setMaxWait(Long.valueOf(propertyResolver.getProperty("second.maxWait")));
        datasource.setMaxActive(Integer.valueOf(propertyResolver.getProperty("second.maxActive")));
        datasource.setMinEvictableIdleTimeMillis(Long.valueOf(propertyResolver.getProperty("second.minEvictableIdleTimeMillis")));
        datasource.setDefaultAutoCommit(true);
        return datasource;
    }

    /***
     * 加载 Hiberante配置信息
     * @return
     */
    public Properties loadProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.database", propertyResolver.getProperty("hibernate.database"));
        properties.put("hibernate.generate-ddl", propertyResolver.getProperty("hibernate.generate-ddl"));
        properties.put("hibernate.hbm2ddl.auto", propertyResolver.getProperty("hibernate.ddl-auto"));
        properties.put("hibernate.dialect", propertyResolver.getProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", propertyResolver.getProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", propertyResolver.getProperty("hibernate.format_sql"));
        properties.put("hibernate.jdbc.batch_size", 50);
        properties.put("hibernate.connection.autocommit",true);
        return properties;
    }

    /**
     * 集成 Hibernate，自身管理事务
     *
     * @return
     */
    @Bean(name = "sessionFactory")
    @Qualifier("sessionFactory")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean se = new LocalSessionFactoryBean();
        se.setDataSource(writeDataSource());
        se.setHibernateProperties(loadProperties());
        se.setPackagesToScan("com");
        return se;
    }

    /**
     * 集成 Hibernate，自身管理事务
     *
     * @return
     */
    @Bean(name = "secondSessionFactory")
    @Qualifier("secondSessionFactory")
    public LocalSessionFactoryBean secondSessionFactory() {
        LocalSessionFactoryBean se = new LocalSessionFactoryBean();
        se.setDataSource(writeSecondDataSource());
        se.setPackagesToScan("com.hongdee");
        se.setHibernateProperties(loadProperties());
        return se;
    }

    @Bean(name = "impJdbcTemplate")
    @Qualifier("impJdbcTemplate")
    public JdbcTemplate impJdbcTemplate(@Qualifier("impDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "jdbcTemplate")
    @Qualifier("jdbcTemplate")
    @Primary
    public JdbcTemplate jdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


}
